using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace Curriculum.Business.LectorDatos
{
	/// <summary>
	/// Summary description for LectorDocente.
	/// </summary>
	public class LectorDocente
	{
		string _path = null;

		public string Path
		{
			set { this._path = value; }
			get { return this._path; }
		}

		public Docente Leer()
		{		
			XmlSerializer s = new XmlSerializer(typeof(Docente));
			TextReader fs;

			this.InicializarPathLectura();

			try
			{
				fs = new StreamReader(this._path);
			}
			catch	
			{
				return null;
			}
			
			XmlReader reader = new XmlTextReader(fs);
          
			// Declare an object variable of the type to be deserialized.
			Docente docente;

			// Use the Deserialize method to restore the object's state.
			docente = (Docente) s.Deserialize(reader);

			fs.Close();

			return docente;
		}

		public void Escribir(Docente docente)
		{
			XmlSerializer serializador = new XmlSerializer(typeof(Docente));
			FileStream writer;

			this.InicializarPathEscritura();		

			writer = new FileStream(_path ,System.IO.FileMode.Create);

			serializador.Serialize(writer,docente);

			writer.Close();
		}

		private void InicializarPathLectura()
		{			
			if (this._path == null)
			{
				string dir=System.Configuration.ConfigurationSettings.AppSettings["pathDirectorioDeTrabajo"];
				this._path = dir + Constants.ARCHIVO_DOCENTE;
			}
		}

		private void InicializarPathEscritura()
		{
			if (this._path == null)
			{
				string dir=System.Configuration.ConfigurationSettings.AppSettings["pathDirectorioDeTrabajo"];
				this._path = dir + Constants.ARCHIVO_DOCENTE;
			}
		}
	}
}
